"ui";
var LOCAL_HISTORY;//本地存储库
var bayeData = {
    zhList: [
    ],
};
var zhanghuIndex;
var uistatus = true;
var flstatus = false;
function initUI() {
    var im = ui.layout(
        <frame id="fra" bg="#ffffffff" w="*" h="auto" padding="2">
            <vertical>
                <horizontal> <TextView text="账号" /><input id="username" w="*" /> </horizontal>
                <horizontal> <TextView text="密码" /><input id="password" w="*" /> </horizontal>
                <horizontal> <TextView text="区服名" /><input id="qufu" w="*" hint="多个区请用英文,分隔" /> </horizontal>
                <horizontal> <TextView text="备注" /><input id="beizhu" w="*" /> </horizontal>
                <horizontal> <button id="insert" text="新增账户" /> <button id="update" text="根据账号修改账户" /><button id="clear" text="清空输入信息" /></horizontal>
                <horizontal> <TextView text="可对列表中选中账户进行修改或删除" /></horizontal>
                <horizontal> <TextView text="账号列表" /> <spinner w="auto" id="zhList" entries="" /></horizontal>
                <horizontal> <button id="delete" text="删除当前账号列表选中账号" /> <button id="clearAll" text="清空所有账号" /></horizontal>
                <horizontal> <CheckBox id="zhixingAll" /> <TextView text="执行全部账号(不勾选则执行当前列表中的选中账号)" /></horizontal>
                <horizontal> <TextView textColor="red" text="请勾选需要执行的任务并保存  不点击保存不会生效" /></horizontal>
                <horizontal> <TextView textColor="red" text="售后及开通vip请联系微信 h1694045410" /></horizontal>
                <horizontal> <button id="start" text="启动脚本" /> <button id="save" text="保存所有配置" /> <TextView text="下滑显示更多配置" /></horizontal>
                <ScrollView bg="#46aefeff" >
                    <vertical>
                        <horizontal>
                            <CheckBox id="junwu" /> <TextView text="军务开关" />
                            <CheckBox id="junwumuzheng" /> <TextView text="奖励牧征军务" />
                            <CheckBox id="junwuxunfang" /> <TextView text="奖励寻访军务" />
                        </horizontal>
                        <horizontal><TextView text="军务次数(默认7次 超过7次会自动购买次数)" /><input id="junwunumber" w="50" inputType="number" /></horizontal>
                        <horizontal> <CheckBox id="fenxiang" /> <TextView text="分享得元宝(10元宝)" /></horizontal>
                        <horizontal> <CheckBox id="dianzan" /> <TextView text="自动点赞天子战(60元宝)" /></horizontal>
                        <horizontal> <CheckBox id="zhengbing" /> <TextView text="自动征兵" /></horizontal>
                        <horizontal> <CheckBox id="qingkongtili" /> <TextView text="自动玩小游戏清空体力" /></horizontal>
                        <horizontal> <CheckBox id="maixyxsp" /> <TextView text="自动购买小游戏商城中的银币物品" /></horizontal>
                        <horizontal> <CheckBox id="chouqian" /> <TextView text="自动做小游戏中的观看广告任务抽签)" /></horizontal>
                        <horizontal> <CheckBox id="richang" /> <TextView text="做日常" /></horizontal>
                        <horizontal> <CheckBox id="panjun" /> <TextView text="自动清理叛军" /></horizontal>
                        <horizontal> <CheckBox id="xunfang" /> <TextView text="自动寻访(只寻访传奇或巾帼英雄)" /></horizontal>
                        <horizontal> <CheckBox id="muzheng" /> <TextView text="自动牧征" /></horizontal>
                        <horizontal> <CheckBox id="qiechuo" /> <TextView text="自动切磋(只会切磋传奇或者巾帼英雄)" /></horizontal>
                        <horizontal> <CheckBox id="mingjiangjiaofeng" /> <TextView text="自动名将交锋" /></horizontal>
                        <horizontal> <CheckBox id="qunyinghui" /> <TextView text="自动群英会" /></horizontal>
                        <horizontal> <CheckBox id="yanyizhilu" /> <TextView text="自动演义之路" /></horizontal>
                        <horizontal> <CheckBox id="biwuchang" /> <TextView text="自动比武场" /></horizontal>
                        <horizontal> <CheckBox id="xuezhanshachang" /> <TextView text="自动血战沙场" /></horizontal>
                        <horizontal> <CheckBox id="fenghuotai" /> <TextView text="自动烽火台" /></horizontal>
                        <horizontal> <CheckBox id="neichengziyuan" /> <TextView text="自动收集内城资源" /></horizontal>
                        <horizontal> <CheckBox id="waichengziyuan" /> <TextView text="自动收集外城资源" /></horizontal>
                        <horizontal> <CheckBox id="bingyingkeji" /> <TextView text="自动升级兵营科技" /></horizontal>
                        <horizontal> <CheckBox id="guanmuyingcai" /> <TextView text="自动广幕英才" /></horizontal>
                        <horizontal> <CheckBox id="wangyue" /> <TextView text="自动封神台王越(战力建议3个100万左右低了打不过)" /></horizontal>
                        <horizontal> <CheckBox id="zhangjiao" /> <TextView text="自动封神台张角(战力建议3个50万左右低了打不过)" /></horizontal>
                        <horizontal> <CheckBox id="matou" /> <TextView text="自动领取在线奖励 码头" /></horizontal>
                        <horizontal> <CheckBox id="yingxiongtai" /> <TextView text="自动购买英雄台银币英雄碎片到五次" /></horizontal>
                        <horizontal> <CheckBox id="cangjingge" /> <TextView text="自动购买藏经阁银币技能碎片到五次" /></horizontal>
                        <horizontal> <CheckBox id="qihuoju" /> <TextView text="自动刷新购买奇货居黄金沙漏*3 10次" /></horizontal>
                        <horizontal> <CheckBox id="jianglingtai" /> <TextView text="自动换取将灵台英雄碎片 默认免费次数换完" /></horizontal>
                        <horizontal> <CheckBox id="zhenbaoge" /> <TextView text="自动珍宝阁购买军帖 消耗免费次数刷新到军帖就买" /></horizontal>
                        <horizontal> <CheckBox id="shuazhangong" /> <TextView text="自动到指定地点刷战功     " /> <TextView text="输入地名" /><input id="diming" w="*" /> </horizontal>
                        <horizontal> <CheckBox id="mizhi" /> <TextView text="自动刷密旨任务(弱国不建议勾选 可能一直白给)" /></horizontal>
                        <horizontal> <CheckBox id="zhanji" /> <TextView text="自动刷战纪任务(弱国不建议勾选 可能一直白给)" /></horizontal>
                        <horizontal> <CheckBox id="tianzi" /> <TextView text="自动天子战" /></horizontal>
                        <horizontal> <CheckBox id="xuncheng" /> <TextView text="自动巡城" /></horizontal>
                        <horizontal> <CheckBox id="kuafu" /> <TextView text="自动跨服远征（自动报名加参战）" /></horizontal>
                        <horizontal> <CheckBox id="zeikou10" /> <TextView text="自动10点贼寇" /></horizontal>
                        <horizontal> <CheckBox id="zeikou17" /> <TextView text="自动17:30贼寇" /></horizontal>
                        <horizontal> <CheckBox id="mojin" /> <TextView text="自动摸金校尉" /></horizontal>
                        <horizontal> <CheckBox id="leitai" /> <TextView text="自动擂台战" /></horizontal>
                        <horizontal> <CheckBox id="qunyingzhengba" /> <TextView text="自动群英争霸" /></horizontal>
                    </vertical>
                </ScrollView>
            </vertical>
        </frame>
    );
    //开始执行
    ui.start.click(function () {
        logRecord("启动脚本")
        // ui.finish()
        ui.fra.visibility = 8; // 完全透明不可见
        // if (!flstatus) {
        // flstatus = true
        threads.start(function () {
            startFloaty()
        })
        // }
        launchApp("霸业");
    })
    //保存设置
    ui.save.click(function () {
        logRecord("保存配置")
        bayeData.zhixingAll = ui.zhixingAll.isChecked()
        bayeData.junwu = ui.junwu.isChecked()
        bayeData.junwumuzheng = ui.junwumuzheng.isChecked()
        bayeData.junwuxunfang = ui.junwuxunfang.isChecked()
        bayeData.junwunumber = ui.junwunumber.text()
        bayeData.fenxiang = ui.fenxiang.isChecked()
        bayeData.dianzan = ui.dianzan.isChecked()
        bayeData.zhengbing = ui.zhengbing.isChecked()
        bayeData.qingkongtili = ui.qingkongtili.isChecked()
        bayeData.maixyxsp = ui.maixyxsp.isChecked()
        bayeData.chouqian = ui.chouqian.isChecked()
        bayeData.richang = ui.richang.isChecked()
        bayeData.panjun = ui.panjun.isChecked()
        bayeData.xunfang = ui.xunfang.isChecked()
        bayeData.muzheng = ui.muzheng.isChecked()
        bayeData.qiechuo = ui.qiechuo.isChecked()
        bayeData.mingjiangjiaofeng = ui.mingjiangjiaofeng.isChecked()
        bayeData.qunyinghui = ui.qunyinghui.isChecked()
        bayeData.yanyizhilu = ui.yanyizhilu.isChecked()
        bayeData.biwuchang = ui.biwuchang.isChecked()
        bayeData.xuezhanshachang = ui.xuezhanshachang.isChecked()
        bayeData.fenghuotai = ui.fenghuotai.isChecked()
        bayeData.neichengziyuan = ui.neichengziyuan.isChecked()
        bayeData.waichengziyuan = ui.waichengziyuan.isChecked()
        bayeData.bingyingkeji = ui.bingyingkeji.isChecked()
        bayeData.guanmuyingcai = ui.guanmuyingcai.isChecked()
        bayeData.wangyue = ui.wangyue.isChecked()
        bayeData.zhangjiao = ui.zhangjiao.isChecked()
        bayeData.matou = ui.matou.isChecked()
        bayeData.yingxiongtai = ui.yingxiongtai.isChecked()
        bayeData.cangjingge = ui.cangjingge.isChecked()
        bayeData.qihuoju = ui.qihuoju.isChecked()
        bayeData.jianglingtai = ui.jianglingtai.isChecked()
        bayeData.zhenbaoge = ui.zhenbaoge.isChecked()
        bayeData.shuazhangong = ui.shuazhangong.isChecked()
        bayeData.mizhi = ui.mizhi.isChecked()
        bayeData.zhanji = ui.zhanji.isChecked()
        bayeData.tianzi = ui.tianzi.isChecked()
        bayeData.xuncheng = ui.xuncheng.isChecked()
        bayeData.kuafu = ui.kuafu.isChecked()
        bayeData.zeikou10 = ui.zeikou10.isChecked()
        bayeData.zeikou17 = ui.zeikou17.isChecked()
        bayeData.mojin = ui.mojin.isChecked()
        bayeData.leitai = ui.leitai.isChecked()
        bayeData.qunyingzhengba = ui.qunyingzhengba.isChecked()
        saveData()
    })
    //新增账户
    ui.insert.click(function () {
        logRecord("新增账户")
        if (!ui.username.text() || !ui.password.text() || !ui.qufu.text()) {
            logRecord("账号密码区服不能为空")
            return
        }
        var user = {}
        user.username = ui.username.text()
        user.password = ui.password.text()
        user.qufu = ui.qufu.text()
        user.beizhu = ui.beizhu.text()
        logRecord(user)
        bayeData.zhList.push(user)
        update_list()
        ui.username.setText("")
        ui.password.setText("")
        ui.qufu.setText("")
        ui.beizhu.setText("")
    })

    //修改账户
    ui.update.click(function () {
        logRecord("修改账户")
        if (!ui.username.text() || !ui.password.text() || !ui.qufu.text()) {
            logRecord("账号密码区服不能为空")
            return
        }
        var user = {}
        user.username = ui.username.text()
        user.password = ui.password.text()
        user.qufu = ui.qufu.text()
        user.beizhu = ui.beizhu.text()
        for (var k = 0; k < bayeData.zhList.length; k++) {
            if (bayeData.zhList[k].username + "" == user.username + "") {
                bayeData.zhList[k] = user
            }
        }
        update_list()
        ui.username.setText("")
        ui.password.setText("")
        ui.qufu.setText("")
        ui.beizhu.setText("")
    })

    //清空输入
    ui.clear.click(function () {
        logRecord("清空信息")
        ui.username.setText("")
        ui.password.setText("")
        ui.qufu.setText("")
        ui.beizhu.setText("")
    })

    //删除选中账号
    ui.delete.click(function () {
        logRecord("删除账户")
        bayeData.zhList.splice(zhanghuIndex, 1);
        update_list()
    })

    //清空所有账户
    ui.clearAll.click(function () {
        logRecord("清空所有账户")
        bayeData.zhList = []
        update_list()
        ui.username.setText("")
        ui.password.setText("")
        ui.qufu.setText("")
        ui.beizhu.setText("")
    })

    //监听选项变化
    var myAdapterListener = new android.widget.AdapterView.OnItemSelectedListener({
        onItemSelected: function (parent, view, position, id) {
            zhanghuIndex = id;
            user = bayeData.zhList[id]
            if (user) {
                ui.username.setText(user.username)
                ui.password.setText(user.password)
                ui.qufu.setText(user.qufu)
                ui.beizhu.setText(user.beizhu)
            }
        }
    })
    ui.zhList.setOnItemSelectedListener(myAdapterListener)


}

// 更新spinner选项
function update_list() {
    let mFavorite = []
    if (bayeData.zhList.length > 0) {
        bayeData.zhList.forEach(function (e) {
            mFavorite.push(e.username + e.beizhu)
        })
    }
    sp = ui.zhList
    adapter = new android.widget.ArrayAdapter(context, android.R.layout.simple_spinner_item, mFavorite);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp.setAdapter(adapter);
}

function initData() {
    LOCAL_HISTORY = storages.create("QWZS");
    let str = LOCAL_HISTORY.get("baye")
    if (str) {
        bayeData = JSON.parse(str)
    }
}
function saveData() {
    LOCAL_HISTORY.put("baye", JSON.stringify(bayeData))
}
/**
 * 
 * @param {输出日志} str 
 */
function logRecord(str) {
    log(str)
}
function initUIShow() {
    if (bayeData.zhixingAll) {
        ui.zhixingAll.setChecked(bayeData.zhixingAll)
        ui.junwu.setChecked(bayeData.junwu)
        ui.junwumuzheng.setChecked(bayeData.junwumuzheng)
        ui.junwuxunfang.setChecked(bayeData.junwuxunfang)
        ui.junwunumber.setText(bayeData.junwunumber)
        ui.fenxiang.setChecked(bayeData.fenxiang)
        ui.dianzan.setChecked(bayeData.dianzan)
        ui.zhengbing.setChecked(bayeData.zhengbing)
        ui.qingkongtili.setChecked(bayeData.qingkongtili)
        ui.maixyxsp.setChecked(bayeData.maixyxsp)
        ui.chouqian.setChecked(bayeData.chouqian)
        ui.richang.setChecked(bayeData.richang)
        ui.panjun.setChecked(bayeData.panjun)
        ui.xunfang.setChecked(bayeData.xunfang)
        ui.muzheng.setChecked(bayeData.muzheng)
        ui.qiechuo.setChecked(bayeData.qiechuo)
        ui.mingjiangjiaofeng.setChecked(bayeData.mingjiangjiaofeng)
        ui.qunyinghui.setChecked(bayeData.qunyinghui)
        ui.yanyizhilu.setChecked(bayeData.yanyizhilu)
        ui.biwuchang.setChecked(bayeData.biwuchang)
        ui.xuezhanshachang.setChecked(bayeData.xuezhanshachang)
        ui.fenghuotai.setChecked(bayeData.fenghuotai)
        ui.neichengziyuan.setChecked(bayeData.neichengziyuan)
        ui.waichengziyuan.setChecked(bayeData.waichengziyuan)
        ui.bingyingkeji.setChecked(bayeData.bingyingkeji)
        ui.guanmuyingcai.setChecked(bayeData.guanmuyingcai)
        ui.wangyue.setChecked(bayeData.wangyue)
        ui.zhangjiao.setChecked(bayeData.zhangjiao)
        ui.matou.setChecked(bayeData.matou)
        ui.yingxiongtai.setChecked(bayeData.yingxiongtai)
        ui.cangjingge.setChecked(bayeData.cangjingge)
        ui.qihuoju.setChecked(bayeData.qihuoju)
        ui.jianglingtai.setChecked(bayeData.jianglingtai)
        ui.zhenbaoge.setChecked(bayeData.zhenbaoge)
        ui.shuazhangong.setChecked(bayeData.shuazhangong)
        ui.mizhi.setChecked(bayeData.mizhi)
        ui.zhanji.setChecked(bayeData.zhanji)
        ui.tianzi.setChecked(bayeData.tianzi)
        ui.xuncheng.setChecked(bayeData.xuncheng)
        ui.kuafu.setChecked(bayeData.kuafu)
        ui.zeikou10.setChecked(bayeData.zeikou10)
        ui.zeikou17.setChecked(bayeData.zeikou17)
        ui.mojin.setChecked(bayeData.mojin)
        ui.leitai.setChecked(bayeData.leitai)
        ui.qunyingzhengba.setChecked(bayeData.qunyingzhengba)
        update_list()
    }
}
initData()
initUI()
initUIShow()
setInterval(() => {
}, 1000);

function startFloaty() {
    logRecord("创建悬浮窗")
    var w = floaty.rawWindow(
        <frame id="fl"  gravity="center">
            <button h="100px" w="150px" id="bt">停止</button>
        </frame>
    );
    w.setPosition(0, 0);
    ui.run(function () {
        w.bt.click(function () {
            w.fl.visibility = 8;
            ui.fra.visibility = 0;
            floaty.closeAll()
            //可以补充返回逻辑
        })
    });
}


